<?php
session_start();
require_once '../includes/config.php';
require_once '../includes/functions.php';

// 检查是否登录为患者
if (!isset($_SESSION['user_id']) || $_SESSION['role'] != 'patient') {
    header("Location: ../login.php");
    exit();
}

// 获取患者信息
$user_id = $_SESSION['user_id'];
$stmt = $conn->prepare("SELECT u.*, p.* FROM users u JOIN patients p ON u.id = p.user_id WHERE u.id = ?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();

// 获取未读通知数量
$stmt = $conn->prepare("SELECT COUNT(*) as count FROM notifications WHERE user_id = ? AND is_read = 0");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
$notification_count = $result->fetch_assoc()['count'];

// 获取统计数据
// 1. 总预约数
$stmt = $conn->prepare("SELECT COUNT(*) as count FROM appointments WHERE patient_id = ?");
$stmt->bind_param("i", $user['patient_id']);
$stmt->execute();
$result = $stmt->get_result();
$total_appointments = $result->fetch_assoc()['count'];

// 2. 完成的预约数
$stmt = $conn->prepare("SELECT COUNT(*) as count FROM appointments WHERE patient_id = ? AND status = 'completed'");
$stmt->bind_param("i", $user['patient_id']);
$stmt->execute();
$result = $stmt->get_result();
$completed_appointments = $result->fetch_assoc()['count'];

// 3. 待付款数量
$stmt = $conn->prepare("SELECT COUNT(*) as count FROM bills WHERE patient_id = ? AND status = 'unpaid'");
$stmt->bind_param("i", $user['patient_id']);
$stmt->execute();
$result = $stmt->get_result();
$unpaid_bills = $result->fetch_assoc()['count'];

// 4. 处方数量
$stmt = $conn->prepare("SELECT COUNT(*) as count FROM prescriptions WHERE patient_id = ?");
$stmt->bind_param("i", $user['patient_id']);
$stmt->execute();
$result = $stmt->get_result();
$prescription_count = $result->fetch_assoc()['count'];

// 获取最近的预约
$stmt = $conn->prepare("
    SELECT a.*, d.specialty, dep.name as department_name, u.name as doctor_name
    FROM appointments a
    JOIN doctors d ON a.doctor_id = d.id
    JOIN departments dep ON d.department_id = dep.id
    JOIN users u ON d.user_id = u.id
    WHERE a.patient_id = ?
    ORDER BY a.appointment_date DESC, a.appointment_time DESC
    LIMIT 5
");
$stmt->bind_param("i", $user['patient_id']);
$stmt->execute();
$recent_appointments = $stmt->get_result();

// 获取最近的通知
$stmt = $conn->prepare("
    SELECT * FROM notifications 
    WHERE user_id = ? 
    ORDER BY created_at DESC 
    LIMIT 5
");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$recent_notifications = $stmt->get_result();
?>

<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>患者面板 - 医院信息系统</title>
    <link rel="stylesheet" href="/assets\css/bootstrap_69c9e605.min.css">
    <link rel="stylesheet" href="/assets\css/style.css">
    <link rel="stylesheet" href="/assets\css/all_de197510.min.css">
    <style>
        .card-counter {
            box-shadow: 2px 2px 10px #DADADA;
            padding: 20px 10px;
            background-color: #fff;
            height: 100px;
            border-radius: 5px;
            transition: .3s linear all;
        }
        .card-counter:hover {
            box-shadow: 4px 4px 20px #DADADA;
            transition: .3s linear all;
        }
        .card-counter i {
            font-size: 5em;
            opacity: 0.2;
        }
        .card-counter .count-numbers {
            position: absolute;
            right: 35px;
            top: 20px;
            font-size: 32px;
            display: block;
        }
        .card-counter .count-name {
            position: absolute;
            right: 35px;
            top: 65px;
            font-style: italic;
            text-transform: capitalize;
            opacity: 0.5;
            display: block;
            font-size: 18px;
        }
        .bg-primary {
            background-color: #1f88be !important;
            color: #FFF;
        }
        .bg-success {
            background-color: #66bb6a !important;
            color: #FFF;
        }
        .bg-warning {
            background-color: #ffb22b !important;
            color: #FFF;
        }
        .bg-danger {
            background-color: #fc4b6c !important;
            color: #FFF;
        }
        .notification-card {
            border-left: 4px solid #1f88be;
        }
        .appointment-card {
            border-left: 4px solid #66bb6a;
        }
        .patient-sidebar {
            background-color: #343a40;
            color: white;
            min-height: 100vh;
        }
        .sidebar-link {
            color: rgba(255, 255, 255, 0.8);
            padding: 15px 20px;
            display: block;
        }
        .sidebar-link:hover {
            color: white;
            text-decoration: none;
            background-color: rgba(255, 255, 255, 0.1);
        }
        .sidebar-link.active {
            background-color: rgba(255, 255, 255, 0.2);
            color: white;
        }
        .main-content {
            background-color: #f8f9fa;
        }
        .status-indicator {
            width: 10px;
            height: 10px;
            border-radius: 50%;
            display: inline-block;
            margin-right: 5px;
        }
        .status-pending { background-color: #ffb22b; }
        .status-confirmed { background-color: #1f88be; }
        .status-completed { background-color: #66bb6a; }
        .status-cancelled { background-color: #fc4b6c; }
    </style>
</head>
<body>
    <div class="container-fluid">
        <div class="row">
            <!-- 侧边栏导航 -->
            <div class="col-md-2 patient-sidebar p-0">
                <div class="text-center p-4">
                    <img src="<?php echo $user['avatar'] ? '../' . $user['avatar'] : '../assets/images/avatar-placeholder.png'; ?>" class="rounded-circle" width="80" height="80" alt="用户头像">
                    <h5 class="mt-2"><?php echo htmlspecialchars($user['name']); ?></h5>
                    <p class="small"><?php echo htmlspecialchars($user['email']); ?></p>
                </div>
                <hr class="bg-light">
                <ul class="nav flex-column">
                    <li class="nav-item">
                        <a class="sidebar-link active" href="index.php">
                            <i class="fas fa-tachometer-alt mr-2"></i> 控制面板
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="sidebar-link" href="appointments.php">
                            <i class="fas fa-calendar-check mr-2"></i> 我的预约
                            <?php if($total_appointments > 0): ?>
                            <span class="badge badge-light float-right"><?php echo $total_appointments; ?></span>
                            <?php endif; ?>
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="sidebar-link" href="book_appointment.php">
                            <i class="fas fa-calendar-plus mr-2"></i> 预约挂号
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="sidebar-link" href="medical_records.php">
                            <i class="fas fa-file-medical mr-2"></i> 病历记录
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="sidebar-link" href="prescriptions.php">
                            <i class="fas fa-prescription-bottle-alt mr-2"></i> 我的处方
                            <?php if($prescription_count > 0): ?>
                            <span class="badge badge-light float-right"><?php echo $prescription_count; ?></span>
                            <?php endif; ?>
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="sidebar-link" href="bills.php">
                            <i class="fas fa-file-invoice-dollar mr-2"></i> 医疗账单
                            <?php if($unpaid_bills > 0): ?>
                            <span class="badge badge-danger float-right"><?php echo $unpaid_bills; ?></span>
                            <?php endif; ?>
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="sidebar-link" href="notifications.php">
                            <i class="fas fa-bell mr-2"></i> 通知消息
                            <?php if($notification_count > 0): ?>
                            <span class="badge badge-danger float-right"><?php echo $notification_count; ?></span>
                            <?php endif; ?>
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="sidebar-link" href="profile.php">
                            <i class="fas fa-user-circle mr-2"></i> 个人资料
                        </a>
                    </li>
                    <li class="nav-item mt-4">
                        <a class="sidebar-link" href="../logout.php">
                            <i class="fas fa-sign-out-alt mr-2"></i> 退出登录
                        </a>
                    </li>
                </ul>
            </div>
            
            <!-- 主要内容区域 -->
            <div class="col-md-10 main-content p-4">
                <div class="d-flex justify-content-between align-items-center mb-4">
                    <h2><i class="fas fa-tachometer-alt mr-2"></i> 患者控制面板</h2>
                    <div>
                        <span class="text-muted"><?php echo date('Y年m月d日'); ?></span>
                        <a href="notifications.php" class="btn btn-outline-secondary ml-2 position-relative">
                            <i class="fas fa-bell"></i>
                            <?php if($notification_count > 0): ?>
                            <span class="badge badge-danger position-absolute" style="top:-5px;right:-5px;"><?php echo $notification_count; ?></span>
                            <?php endif; ?>
                        </a>
                    </div>
                </div>
                
                <!-- 统计卡片 -->
                <div class="row mb-4">
                    <div class="col-md-3">
                        <div class="card-counter bg-primary">
                            <i class="fas fa-calendar-check"></i>
                            <span class="count-numbers"><?php echo $total_appointments; ?></span>
                            <span class="count-name">总预约数</span>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="card-counter bg-success">
                            <i class="fas fa-check-circle"></i>
                            <span class="count-numbers"><?php echo $completed_appointments; ?></span>
                            <span class="count-name">已完成</span>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="card-counter bg-warning">
                            <i class="fas fa-prescription-bottle-alt"></i>
                            <span class="count-numbers"><?php echo $prescription_count; ?></span>
                            <span class="count-name">处方数</span>
                        </div>
                    </div>
                    <div class="col-md-3">
                        <div class="card-counter bg-danger">
                            <i class="fas fa-file-invoice-dollar"></i>
                            <span class="count-numbers"><?php echo $unpaid_bills; ?></span>
                            <span class="count-name">待付款</span>
                        </div>
                    </div>
                </div>
                
                <div class="row">
                    <!-- 最近预约列表 -->
                    <div class="col-md-7">
                        <div class="card mb-4">
                            <div class="card-header bg-white">
                                <h5 class="mb-0"><i class="fas fa-calendar-check mr-2 text-primary"></i>最近预约</h5>
                            </div>
                            <div class="card-body p-0">
                                <div class="list-group list-group-flush">
                                    <?php if($recent_appointments->num_rows > 0): ?>
                                        <?php while($appointment = $recent_appointments->fetch_assoc()): ?>
                                            <a href="appointment_details.php?id=<?php echo $appointment['id']; ?>" class="list-group-item list-group-item-action">
                                                <div class="d-flex w-100 justify-content-between">
                                                    <h6 class="mb-1">
                                                        <span class="status-indicator status-<?php echo $appointment['status']; ?>"></span>
                                                        <?php echo htmlspecialchars($appointment['department_name']); ?> - 
                                                        <?php echo htmlspecialchars($appointment['doctor_name']); ?>
                                                        <small>(<?php echo htmlspecialchars($appointment['specialty']); ?>)</small>
                                                    </h6>
                                                    <small><?php echo format_date($appointment['appointment_date']); ?> <?php echo htmlspecialchars($appointment['appointment_time']); ?></small>
                                                </div>
                                                <p class="mb-1 small text-muted">
                                                    预约状态: 
                                                    <?php
                                                        $status_labels = [
                                                            'pending' => '<span class="badge badge-warning">待确认</span>',
                                                            'confirmed' => '<span class="badge badge-primary">已确认</span>',
                                                            'completed' => '<span class="badge badge-success">已完成</span>',
                                                            'cancelled' => '<span class="badge badge-danger">已取消</span>'
                                                        ];
                                                        echo $status_labels[$appointment['status']];
                                                    ?>
                                                </p>
                                            </a>
                                        <?php endwhile; ?>
                                    <?php else: ?>
                                        <div class="list-group-item">
                                            <p class="text-center text-muted my-3">您还没有任何预约记录</p>
                                            <div class="text-center mb-3">
                                                <a href="book_appointment.php" class="btn btn-primary btn-sm">
                                                    <i class="fas fa-calendar-plus mr-1"></i> 立即预约
                                                </a>
                                            </div>
                                        </div>
                                    <?php endif; ?>
                                </div>
                                <?php if($recent_appointments->num_rows > 0): ?>
                                <div class="card-footer bg-white text-center">
                                    <a href="appointments.php" class="btn btn-outline-primary btn-sm">查看全部预约</a>
                                </div>
                                <?php endif; ?>
                            </div>
                        </div>
                        
                        <!-- 快速操作卡片 -->
                        <div class="card">
                            <div class="card-header bg-white">
                                <h5 class="mb-0"><i class="fas fa-bolt mr-2 text-warning"></i>快速操作</h5>
                            </div>
                            <div class="card-body">
                                <div class="row">
                                    <div class="col-6 col-md-3 text-center mb-3">
                                        <a href="book_appointment.php" class="btn btn-outline-primary btn-block py-3">
                                            <i class="fas fa-calendar-plus mb-2 d-block" style="font-size: 1.5rem;"></i>
                                            预约挂号
                                        </a>
                                    </div>
                                    <div class="col-6 col-md-3 text-center mb-3">
                                        <a href="medical_records.php" class="btn btn-outline-info btn-block py-3">
                                            <i class="fas fa-file-medical mb-2 d-block" style="font-size: 1.5rem;"></i>
                                            病历记录
                                        </a>
                                    </div>
                                    <div class="col-6 col-md-3 text-center mb-3">
                                        <a href="prescriptions.php" class="btn btn-outline-success btn-block py-3">
                                            <i class="fas fa-prescription-bottle-alt mb-2 d-block" style="font-size: 1.5rem;"></i>
                                            我的处方
                                        </a>
                                    </div>
                                    <div class="col-6 col-md-3 text-center mb-3">
                                        <a href="bills.php" class="btn btn-outline-danger btn-block py-3">
                                            <i class="fas fa-file-invoice-dollar mb-2 d-block" style="font-size: 1.5rem;"></i>
                                            医疗账单
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                    
                    <!-- 最近通知和用户信息 -->
                    <div class="col-md-5">
                        <!-- 个人信息卡片 -->
                        <div class="card mb-4">
                            <div class="card-header bg-white">
                                <h5 class="mb-0"><i class="fas fa-user-circle mr-2 text-info"></i>个人信息</h5>
                            </div>
                            <div class="card-body">
                                <div class="d-flex mb-3">
                                    <img src="<?php echo $user['avatar'] ? '../' . $user['avatar'] : '../assets/images/avatar-placeholder.png'; ?>" class="rounded-circle mr-3" width="60" height="60" alt="用户头像">
                                    <div>
                                        <h5 class="mb-1"><?php echo htmlspecialchars($user['name']); ?></h5>
                                        <p class="text-muted mb-1">
                                            <i class="fas fa-id-card mr-1"></i> 
                                            患者ID: <?php echo htmlspecialchars($user['patient_id']); ?>
                                        </p>
                                        <p class="text-muted mb-0">
                                            <i class="fas fa-phone mr-1"></i> 
                                            <?php echo htmlspecialchars($user['phone']); ?>
                                        </p>
                                    </div>
                                </div>
                                <div class="row">
                                    <div class="col-md-6">
                                        <p class="mb-1"><strong>性别:</strong> <?php echo $user['gender'] == 'male' ? '男' : '女'; ?></p>
                                        <p class="mb-1"><strong>年龄:</strong> <?php echo calculate_age($user['birthday']); ?>岁</p>
                                        <p class="mb-1"><strong>血型:</strong> <?php echo htmlspecialchars($user['blood_type'] ?: '未知'); ?></p>
                                    </div>
                                    <div class="col-md-6">
                                        <p class="mb-1"><strong>过敏史:</strong> <?php echo htmlspecialchars($user['allergies'] ?: '无'); ?></p>
                                        <p class="mb-1"><strong>慢性病:</strong> <?php echo htmlspecialchars($user['chronic_diseases'] ?: '无'); ?></p>
                                        <p class="mb-1"><strong>家族病史:</strong> <?php echo htmlspecialchars($user['family_medical_history'] ?: '无'); ?></p>
                                    </div>
                                </div>
                                <div class="text-center mt-3">
                                    <a href="profile.php" class="btn btn-outline-primary btn-sm">
                                        <i class="fas fa-edit mr-1"></i> 修改资料
                                    </a>
                                </div>
                            </div>
                        </div>
                        
                        <!-- 通知卡片 -->
                        <div class="card">
                            <div class="card-header bg-white">
                                <h5 class="mb-0">
                                    <i class="fas fa-bell mr-2 text-warning"></i>最新通知
                                    <?php if($notification_count > 0): ?>
                                    <span class="badge badge-danger"><?php echo $notification_count; ?></span>
                                    <?php endif; ?>
                                </h5>
                            </div>
                            <div class="card-body p-0">
                                <div class="list-group list-group-flush">
                                    <?php if($recent_notifications->num_rows > 0): ?>
                                        <?php while($notification = $recent_notifications->fetch_assoc()): ?>
                                            <a href="notification_details.php?id=<?php echo $notification['id']; ?>" class="list-group-item list-group-item-action <?php echo $notification['is_read'] ? '' : 'bg-light'; ?>">
                                                <div class="d-flex w-100 justify-content-between">
                                                    <h6 class="mb-1">
                                                        <?php if(!$notification['is_read']): ?>
                                                        <span class="badge badge-primary mr-1">新</span>
                                                        <?php endif; ?>
                                                        <?php echo htmlspecialchars($notification['title']); ?>
                                                    </h6>
                                                    <small><?php echo time_elapsed_string($notification['created_at']); ?></small>
                                                </div>
                                                <p class="mb-1 small text-truncate"><?php echo htmlspecialchars($notification['content']); ?></p>
                                            </a>
                                        <?php endwhile; ?>
                                    <?php else: ?>
                                        <div class="list-group-item">
                                            <p class="text-center text-muted my-3">您目前没有任何通知</p>
                                        </div>
                                    <?php endif; ?>
                                </div>
                                <?php if($recent_notifications->num_rows > 0): ?>
                                <div class="card-footer bg-white text-center">
                                    <a href="notifications.php" class="btn btn-outline-primary btn-sm">查看所有通知</a>
                                </div>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="/assets\js/jquery.min.js"></script></script></script>
    <script src="/assets\js/bootstrap.bundle.min.js"></script></script></script>
</body>
</html> 